package cn.iocoder.yudao.module.system.dal.mysql.equivalentscorerank;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.system.dal.dataobject.equivalentscorerank.EquivalentScoreDetailDO;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;

/**
 * 等效分数详情 Mapper
 *
 * @author 芋道源码
 */
@Mapper
public interface EquivalentScoreDetailMapper extends BaseMapperX<EquivalentScoreDetailDO> {

    /**
     * 根据等效分数排名记录ID查询详情列表
     *
     * @param equivalentScoreRankId 等效分数排名记录ID
     * @return 详情列表
     */
    default List<EquivalentScoreDetailDO> selectByEquivalentScoreRankId(Long equivalentScoreRankId) {
        return selectList(new LambdaQueryWrapperX<EquivalentScoreDetailDO>()
                .eq(EquivalentScoreDetailDO::getEquivalentScoreRankId, equivalentScoreRankId)
                .orderByAsc(EquivalentScoreDetailDO::getYear));
    }

    /**
     * 根据等效分数排名记录ID和年份查询详情
     *
     * @param equivalentScoreRankId 等效分数排名记录ID
     * @param year 年份
     * @return 详情记录
     */
    default EquivalentScoreDetailDO selectByEquivalentScoreRankIdAndYear(Long equivalentScoreRankId, Integer year) {
        return selectOne(new LambdaQueryWrapperX<EquivalentScoreDetailDO>()
                .eq(EquivalentScoreDetailDO::getEquivalentScoreRankId, equivalentScoreRankId)
                .eq(EquivalentScoreDetailDO::getYear, year));
    }

    /**
     * 根据年份查询所有详情记录
     *
     * @param year 年份
     * @return 详情记录列表
     */
    default List<EquivalentScoreDetailDO> selectByYear(Integer year) {
        return selectList(new LambdaQueryWrapperX<EquivalentScoreDetailDO>()
                .eq(EquivalentScoreDetailDO::getYear, year)
                .orderByDesc(EquivalentScoreDetailDO::getCreateTime));
    }

    /**
     * 根据等效分数排名记录ID删除所有详情记录
     *
     * @param equivalentScoreRankId 等效分数排名记录ID
     * @return 删除的记录数
     */
    default int deleteByEquivalentScoreRankId(Long equivalentScoreRankId) {
        return delete(new LambdaQueryWrapperX<EquivalentScoreDetailDO>()
                .eq(EquivalentScoreDetailDO::getEquivalentScoreRankId, equivalentScoreRankId));
    }

    /**
     * 批量插入详情记录
     *
     * @param details 详情记录列表
     * @return 插入的记录数
     */
    default int insertBatch(List<EquivalentScoreDetailDO> details) {
        if (details == null || details.isEmpty()) {
            return 0;
        }
        int count = 0;
        for (EquivalentScoreDetailDO detail : details) {
            insert(detail);
            count++;
        }
        return count;
    }

    /**
     * 检查是否存在指定的详情记录
     *
     * @param equivalentScoreRankId 等效分数排名记录ID
     * @param year 年份
     * @return 是否存在
     */
    default boolean existsByEquivalentScoreRankIdAndYear(Long equivalentScoreRankId, Integer year) {
        return selectCount(new LambdaQueryWrapperX<EquivalentScoreDetailDO>()
                .eq(EquivalentScoreDetailDO::getEquivalentScoreRankId, equivalentScoreRankId)
                .eq(EquivalentScoreDetailDO::getYear, year)) > 0;
    }

    /**
     * 根据等效分数排名记录ID列表批量删除详情记录（用于清除导入的数据）
     *
     * @param equivalentScoreRankIds 等效分数排名记录ID列表
     * @return 删除的记录数
     */
    default int deleteByEquivalentScoreRankIds(List<Long> equivalentScoreRankIds) {
        if (equivalentScoreRankIds == null || equivalentScoreRankIds.isEmpty()) {
            return 0;
        }
        return delete(new LambdaQueryWrapperX<EquivalentScoreDetailDO>()
                .in(EquivalentScoreDetailDO::getEquivalentScoreRankId, equivalentScoreRankIds));
    }

}